/**
 * <strong>Title : MaterialTypeController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.platform.web.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015-9-10 上午09:57:19<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.cm.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.MaterialType;
import cn.b.sky.mes.cm.model.condition.MaterialTypeCondition;
import cn.b.sky.mes.cm.service.MaterialTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : MaterialTypeController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.platform.web.controller<br></strong>
 * <strong>Description : </strong>@物料类型控制器，提供工厂发布，查询功能@<br> 
 * <strong>Create on : 2015-9-10 上午09:57:19<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */

@Controller
@RequestMapping(value = "/materialType/*")
public class MaterialTypeController extends SecurityController {
  @Resource
  private MaterialTypeService materialTypeService;

  @RequestMapping(value = "materialTypeList")
  public String materialTypeList(Model model, @ModelAttribute("condition") MaterialTypeCondition materialTypeCondition,
      String navTabId) {
    materialTypeCondition.setSiteCode(getSiteCode());
    if (StringUtils.isEmpty(materialTypeCondition.getOrderField())) {
      materialTypeCondition.setOrderField("createDateTime");
    }
    List<MaterialType> materialTypes = materialTypeService.selectMaterialTypeList(materialTypeCondition);
    int count = materialTypeService.getMaterialTypeCount(materialTypeCondition);
    materialTypeCondition.setData(materialTypes);
    materialTypeCondition.setTotalCount(count);
    model.addAttribute("page", materialTypeCondition);
    model.addAttribute(NAVTABID, navTabId);
    return "cm/materialType/materialType_list";
  }

  @RequestMapping(value = "addMaterialType")
  public String addMaterialType(String navTabId, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "add");
    return "cm/materialType/materialType_add";
  }

  @RequestMapping(value = "modifyMaterialType")
  public String modifyMaterialType(String navTabId, String id, Model model) {
    MaterialType materialType = materialTypeService.getMaterialType(id, getSiteCode());
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("materialType", materialType);
    model.addAttribute("pattern", "modify");
    return "cm/materialType/materialType_add";
  }

  @RequestMapping(value = "viewMaterialType")
  public String viewMaterialType(String navTabId, String id, Model model) {

    MaterialType materialType = materialTypeService.getMaterialType(id, getSiteCode());
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("materialType", materialType);
    model.addAttribute("pattern", "read");
    return "cm/materialType/materialType_add";

  }

  @RequestMapping(value = "validateMaterialTypeCode")
  @ResponseBody
  public boolean validateMaterialTypeCode(String materialTypeCode, Model model) {
    if (!StringUtils.isBlank(materialTypeCode)) {
      MaterialTypeCondition materialTypeCondition = new MaterialTypeCondition();
      materialTypeCondition.setMaterialTypeCode(materialTypeCode);
      materialTypeCondition.setSiteCode(SecurityContextUtil.getSiteCode());
      int count = materialTypeService.getMaterialTypeExactCount(materialTypeCondition);
      //MaterialType mt=materialTypeService.getMaterialType(materialTypeCondition);
      return count == 0;
    }
    return true;
  }

  @RequestMapping(value = "saveMaterialType")
  @ResponseBody
  public DWZResult saveMaterialType(Model model, String navTabId, String id, MaterialType materialType,
      HttpServletRequest request) {
    if (materialType == null) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    setCreateInfo(materialType);
    materialType.setSiteCode(materialType.getSiteCode());
    if (StringUtils.isBlank(id)) {
      materialTypeService.insertMaterialType(materialType);
      String forwardUrl = getForwardUrl(request) + "/materialType/materialTypeList?navTabId=" + navTabId;
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, forwardUrl, null);
    } else {
      materialTypeService.updateMaterialType(materialType);
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
    }

  }

  @RequestMapping("deleteMaterialType")
  @ResponseBody
  public DWZResult deleteMaterialType(Model model, String navTabId, String id, HttpServletRequest request) {

    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String[] materialTypeCodes = ids.split(",");
    int len = materialTypeCodes.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      reports[i] = materialTypeService.deleteMaterialTypes(materialTypeCodes[i], getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }

  }

}
